package android.FiltrosJava;

import android.graphics.Bitmap;

public abstract class PointFilter {

	public int delimitaCor(int c){
		if (c < 0)
			return 0;
		if (c > 255)
			return 255;
		return c;
	}

	public Bitmap applyFilter(Bitmap bmp) {

		int[] pixels;
		int height = bmp.getHeight();
		int width = bmp.getWidth();

		pixels = new int[height * width];
		bmp.getPixels(pixels, 0, width, 0, 0, width, height);// ofset(2o param)
		// indice d
		// ecome�o das
		// linhas, 3o
		// param:
		// intervalo de
		// cada linha do
		// array pixels

		int[] vetPix = new int[4];

		// BrightFilter BFilter= new BrightFilter(100);

		int c = 0;
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {

				// int2Rgbvet(vetPix, pixels[c]);

				vetPix[3] = (pixels[c] >> 24);
				vetPix[2] = ((pixels[c] & 0x00FF0000) >> 16);
				vetPix[1] = ((pixels[c] & 0x0000FF00) >> 8);
				vetPix[0] = (pixels[c] & 0x000000FF);
				if (c == 0) {
					System.out.println("valor pixel= " + pixels[c]);
					for (int i = 0; i < 4; i++) {
						System.out.println("-->" + vetPix[i]);
					}
				}

				vetPix = computeFilter(x, y, vetPix);

				// pixels[c]= rgb2Int(vetPix);
				pixels[c] = ((vetPix[3] & 0xFF) << 24)
						| ((vetPix[2] & 0xFF) << 16)
						| ((vetPix[1] & 0xFF) << 8) | (vetPix[0] & 0xFF);

				if (c == 0) {
					System.out.println("valor pixel= " + pixels[c]);
					for (int i = 0; i < 4; i++) {
						System.out.println("-->" + vetPix[i]);
					}
				}
				c++;
			}
		}

		Bitmap novo = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		// novo.setPixels(pixels, offset, stride, x, y, width, height)
		novo.setPixels(pixels, 0, width, 0, 0, width, height);
		if (bmp != null) {
			bmp.recycle();
		}
		pixels = null;
		return novo;
	}

	/*
	 * 
	 * public BufferedImage applyFilter(BufferedImage src){ int width =
	 * src.getWidth(); int height = src.getHeight();
	 * 
	 * WritableRaster raster_src = src.getRaster(); int numBands =
	 * raster_src.getNumBands(); int[] imageData = new int[numBands];
	 * 
	 * for ( int y = 0; y < height; y++ ) { for ( int x = 0; x < width; x++ ) {
	 * raster_src.getPixel(x,y,imageData); imageData = computeFilter(x, y,
	 * imageData); raster_src.setPixel(x,y,imageData); } }
	 * 
	 * return src; }
	 */

	public abstract int[] computeFilter(int px, int py, int[] imageData);

}
